<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doUpload.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. <br>
    <%-- <%
    	String r=request.getParameter("uname");
    	String id=request.getParameter("idcard");
       out.print("name="+r+"<br/>");
       out.print("id="+id+"<br/>");
     %>
     <%
       ServletInputStream in=request.getInputStream();
       int ch=-1;
       while((ch=in.read())!=-1){
         out.print((char)ch);
       }
      %> --%>
     <%
        ///request.setCharacterEncoding("utf-8");
     	DiskFileItemFactory factory = new DiskFileItemFactory();
		// 内存存储的最大值
		factory.setSizeThreshold(4096);
		
		//factory.setRepository(tempPath);
 
		ServletFileUpload upload = new ServletFileUpload(factory);
 		//upload.setHeaderEncoding("utf-8");
 		List<FileItem> list= upload.parseRequest(request);
 		for(FileItem f:list){
 		  out.print("表单元素名："+f.getFieldName());
 		  out.print("文件名："+f.getName());
 		  out.print("是否是表单元素："+f.isFormField());
 		  if(f.isFormField())
 		     out.print("，value="+f.getString("utf-8"));
 		   else
 		   	{
 		   	  if(f.getSize()<=0)
 		   	     continue;
 		   	   
 		   	  String realpath=application.getRealPath("images");
 		   	  String fileName=UUID.randomUUID().toString();//Math.random();
 		   	  String houzhui=f.getName().substring(f.getName().lastIndexOf('.'));
 		   	  String newName=fileName+houzhui;//保存的文件名
 		   	  f.write(new java.io.File(realpath,newName)); 
 		   	  //f.write(new java.io.File("d:/"+f.getName())); 
 		   	  out.print("文件爆粗了");
 		   	  out.print("<img src='images/"+newName+"'/>");
 		   	} 
 		  out.print("<br/>");
      } %>
  </body>
</html>
